package com.ruoyi.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Example;
import io.swagger.annotations.ExampleProperty;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@Api(tags = "用户注册接口")
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @ApiOperation(
        value = "用户注册", 
        notes = "用于新用户注册系统账号，需要验证用户名唯一性，密码长度，以及验证码正确性",
        response = AjaxResult.class,
        responseReference = "{\"成功\":{\"code\":200,\"msg\":\"操作成功\",\"data\":null},\"失败\":{\"code\":500,\"msg\":\"注册失败，验证码错误/用户名已存在/密码不符合要求等具体错误信息\"}}"
    )
    @ApiResponses({
        @io.swagger.annotations.ApiResponse(
            code = 200, 
            message = "操作成功", 
            response = AjaxResult.class,
            examples = @Example(value = {
                @ExampleProperty(mediaType = "application/json", value = "{\"code\":200,\"msg\":\"操作成功\"}")
            }),
            reference = "返回字段说明：\n- code: 状态码，200表示成功\n- msg: 提示信息\n"
        ),
        @io.swagger.annotations.ApiResponse(
            code = 500, 
            message = "操作失败具体的信息"
        )
    })
    @ApiImplicitParam(
        name = "user", 
        value = "用户注册信息", 
        required = true, 
        dataType = "RegisterBody",
        example = "{\"username\":\"test123\",\"password\":\"Test123456\",\"code\":\"1234\"}"
    )
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
}
